package com.jinjiafeng.enjoyjoke;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jinjiafeng.baselibrary.inject.BindView;
import com.jinjiafeng.baselibrary.widget.indicator.BaseIndicatorAdapter;
import com.jinjiafeng.baselibrary.widget.indicator.IndicatorView;
import com.jinjiafeng.framelibrary.BaseSkinActivity;

import java.util.ArrayList;

public class MainActivity extends BaseSkinActivity {
    private static final String TAG = "MainActivity";
    private final String[] mTitles = {"首页", "热门", "推荐", "游戏", "广告", "新闻", "娱乐", "影视", "图片"};
    @BindView(R.id.indicator)
    IndicatorView mIndicatorView;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    private ArrayList<Fragment> mFragments;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        mFragments = new ArrayList<>();
        for (String title : mTitles) {
            mFragments.add(TitleFragment.newInstance(title));
        }
        mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    }

    @Override
    protected void initData() {
        mIndicatorView.setAdapter(new BaseIndicatorAdapter<TextView>() {
            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public TextView getView(int position, ViewGroup viewGroup) {
                final TextView textView = new AppCompatTextView(MainActivity.this);
                textView.setText(mTitles[position]);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                textView.setPadding(20,20,20,20);
                textView.setTextColor(Color.BLACK);
                textView.setGravity(Gravity.CENTER);
                return textView;
            }

            @Override
            public void highlightView(TextView view) {
                view.setTextColor(Color.RED);
            }

            @Override
            public void restoreView(TextView view) {
                view.setTextColor(Color.BLACK);
            }

            @Override
            public View getBottomTrackView() {
                final View view = new View(MainActivity.this);
                view.setBackgroundColor(Color.RED);
                final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(80, 8);
                view.setLayoutParams(params);
                return view;
            }
        },mViewPager);
    }

    private class MyAdapter extends FragmentPagerAdapter{

        MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mTitles.length ;
        }
    }
}
